package bairui.international.console.web.controller.advance.product;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

import com.lianjing.sh.model.uml.model.status.State_UpDown;
import com.lianjing.sh.project.config.web.Project_Config_Web_PageTemplate;
import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.module.Module;
import com.lianjing.sh.spring.web.controller.enhance.module.context.ModuleContext;
import com.lianjing.sh.spring.web.controller.enhance.navigations.context.NavigationContext;
import com.querydsl.core.BooleanBuilder;

import bairui.international.common.config.Config_Setup;
import bairui.international.console.web.controller.advance.C_Advance_Index_Left;
import bairui.international.console.web.util.Util_ProductCategory;
import bairui.international.model.domain.jpa.TB_Product;
import bairui.international.model.domain.jpa.TB_ProductProperty;
import bairui.international.model.domain.jpa.TB_ProductPropertyItem;
import bairui.international.model.type.Type_ProductProperty;
import bairui.international.service.Service_Product;
import bairui.international.service.Service_ProductCategory;
import bairui.international.service.Service_ProductProperty;
import bairui.international.service.Service_ProductPropertyItem;

/**
 *
 * 物料编辑
 *
 * @generated
 * 
 * @see STest_C_Advance_Product_Edit_EditAccess 测试
 *
 */
@Controller
public class C_Advance_Product_Edit_EditAccess
 extends SC_Advance_Product_Edit_EditAccess 
{
	
	@Autowired private Service_ProductPropertyItem service_ProductPropertyItem;
	@Autowired private Service_ProductProperty service_ProductProperty;
	@Autowired private Service_Product service_Product;
	@Autowired private Service_ProductCategory service_ProductCategory;
	
	
	/**
	 * 访问编辑页面
	 */
	@Action(EditAccess.class)
	@GetMapping(EditAccess.RequestMappingValue)
	public ModelAndView editAccess(
		ModelMap map, HttpServletRequest request,
		NavigationContext navigationContext, ModuleContext moduleContext, 
		@PathVariable(name=EditAccess.PathVariableName.Id) long id
	) throws Exception {
		/* 侧边栏 */
		C_Advance_Index_Left.importPage(map);
		//
		TB_Product bean = service_Product.findById(id);
		bean.setImgUrl(new C_Advance_Product_Show_ShowImg.ShowImg.Operate(){
			@Override public Object getId() {
				return id;
			}
		}.getUrl());
		/* 模块 */
		moduleContext
			.addModule(module_Edit.module(
					bean,
					new C_Advance_Product_Edit_Edit.Edit.Operate(){
						@Override public Object getId() { return id; }
					},
					Util_ProductCategory.treeToList(service_ProductCategory
							.list(it->
								new BooleanBuilder()
									.and(it.name.ne(Config_Setup.Barter).and(it.root.name.ne(Config_Setup.Barter)))
									.and(it.name.ne(Config_Setup.Wrap).and(it.root.name.ne(Config_Setup.Wrap)))
							)
						)
				)) 	
			.addModule(this.module_RollPage.module(
					new C_Advance_Product_RollManage_Page.Page.Operate(){
						@Override public Object getId() {
							return id;
						}
					}, 
					new C_Advance_Product_RollManage_FileUpload.FileUpload.Operate(){
						@Override public Object getId() {
							return id;
						}
					},  
					new C_Advance_Product_RollManage_EditAccess.EditAccess.Operate(){
						@Override public Object getId() {
							return id;
						}
						@Override public Object getImgId() {
							return "{{:id}}";
						}
					},  
					new C_Advance_Product_RollManage_Publish.Publish.Operate(){
						@Override public Object getId() {
							return id;
						}
						@Override public Object getImgId() {
							return "{{:id}}";
						}
					}, 
					new C_Advance_Product_RollManage_Up.Up.Operate(){
						@Override public Object getId() {
							return id;
						}
						@Override public Object getImgId() {
							return "{{:id}}";
						}
					}, 
					new C_Advance_Product_RollManage_Down.Down.Operate(){
						@Override public Object getId() {
							return id;
						}
						@Override public Object getImgId() {
							return "{{:id}}";
						}
					},  
					new C_Advance_Product_RollManage_Delete.Delete.Operate(){
						@Override public Object getId() {
							return id;
						}
						@Override public Object getImgId() {
							return "{{:id}}";
						}
					}
				));
			/*
			 * 编辑属性
			 */
			long categoryId = bean.getProductCategory().getId();
			List<TB_ProductProperty> ps = service_ProductProperty.list(it->it.productCategoryProperties.any().productCategory.id.eq(categoryId).and(it.state.eq(State_UpDown.Uped)));
			List<Module> modules = new ArrayList<>();
			//
			for(TB_ProductProperty property : ps){
				if(property.getType()== Type_ProductProperty.Rang){
					continue;
				}
				TB_ProductPropertyItem pItem = service_ProductPropertyItem.findOne(it->it.productProperty.eq(property).and(it.products.any().eq(bean)));
				//
				modules.add(module_ProductPropertyEdit.module(
								new Module_ProductPropertyEdit.Model(bean, property.getName(), pItem),
								new C_Advance_Product_Edit_PropertyEdit.PropertyEdit.Operate(){
									@Override public Object getId() { return id; }
									public com.lianjing.sh.spring.web.controller.base.operate.OperateSupport returnTo() {
										return new C_Advance_Product_Edit_EditAccess.EditAccess.Operate() {
											@Override public Object getId() {
												return id;
											}
										};
									};
								},
								service_ProductPropertyItem.list(it->it.productProperty.eq(property).and(it.state.eq(State_UpDown.Uped)))
							));
			}
			moduleContext
				.addModule(modules.toArray(new Module[]{}));
			/*
			 * 编辑详情
			 */
			moduleContext
				.addModule(this.module_DetailEdit.module(
						bean, 
						new C_Advance_Product_Edit_DetailEdit.DetailEdit.Operate(){
							@Override public Object getId() {
								return id;
							}
						}
					));
		//
		return Project_Config_Web_PageTemplate.Admin_Common(request);
	}
	

}
